package com.easy.controller;

import com.easy.bean.Detail;
import com.easy.common.CommonResult;
import com.easy.service.IDetailService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class DetailController {

    @Autowired
    IDetailService detailService;

    //通过ID获取
    @GetMapping("deliverydetail/{id}")
    public Detail getDeliveryDetailByID(@PathVariable int id) {
        return detailService.getDeliveryDetailByID(id);
    }
    @GetMapping("receiptdetail/{id}")
    public Detail getReceiptDetailByID(@PathVariable int id) {
        return detailService.getReceiptDetailByID(id);
    }

    //获取列表
    @GetMapping("deliverydetail")
    @Transactional
    public CommonResult getDeliveryDetail(Detail detail, Page page) {
        List<Detail> list =detailService.getDeliveryDetail(detail,page);
        int count=detailService.getDeliveryCount(detail);
        HashMap map=new HashMap();
        map.put("list",list);
        map.put("count",count);
        return CommonResult.success(map);
    }
    @GetMapping("receiptdetail")
    @Transactional
    public CommonResult getReceiptDetail(Detail detail, Page page) {
        List<Detail> list =detailService.getReceiptDetail(detail,page);
        int count=detailService.getReceiptCount(detail);
        HashMap map=new HashMap();
        map.put("list",list);
        map.put("count",count);
        return CommonResult.success(map);
    }


    //删除
    @DeleteMapping("deliverydetail/{id}")
    public CommonResult delDeliveryDetail(@PathVariable int id) {
        int count = detailService.delDeliveryDetail(id);
        if(count>0){
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }

    }
    @DeleteMapping("receiptdetail/{id}")
    public CommonResult delReceiptDetail(@PathVariable int id) {
        int count = detailService.delReceiptDetail(id);
        if(count>0){
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }

    }

    //新增JSON
    @PostMapping("deliverydetail")
    @Transactional
    public CommonResult addDeliveryDetail(@RequestBody Detail detail){
        detailService.addDeliveryDetail(detail);
        detail = detailService.getDeliveryDetailByID(detail.getId());
        return CommonResult.success(detail);
    }
    @PostMapping("receiptdetail")
    @Transactional
    public CommonResult addReceiptDetail(@RequestBody Detail detail){
        detailService.addReceiptDetail(detail);
        detail = detailService.getReceiptDetailByID(detail.getId());
        return CommonResult.success(detail);
    }


}
